package zh.demo.utils;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * Created by zhangheng on 2017/11/3.
 */
public class MaptoEntityUtils {

    public static void main(String[] args) {

    }

    public static Object MaptoObject(Map<Object, Object> map, Object object) throws IllegalAccessException {
        Class oclass = object.getClass();
        Field[] fields = oclass.getDeclaredFields();
        if(!map.isEmpty()){
            Set<Object> set = map.keySet();
            Iterator<Object> it = set.iterator();
            while (it.hasNext()){
                String key = it.next().toString();
                for (Field field : fields) {
                    field.setAccessible(true);
                    String name = field.getName();
                    String type = field.getType().getName();
                    if(key.equals(name)){
                        if("java.lang.Long".equals(type)){
                            if(map.get(key)!=null) {
                                field.set(object, Long.valueOf(map.get(key).toString()));
                            }
                        }else{
                            field.set(object, map.get(key));
                        }

                        break;
                    }
                }
            }
        }
        return object;
    }
}
